//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory 2010
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add View" recipe.
//
// A presenter calls methods of a view to update the information that the view displays. 
// The view exposes its methods through an interface definition, and the presenter contains
// a reference to the view interface. This allows you to test the presenter with different 
// implementations of a view (for example, a mock view).
//
// For more information see:
// ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.scsf.2006jun/SCSF/html/03-030-Model%20View%20Presenter%20%20MVP%20.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using GlobalBank.BranchSystems.Interface.Services;
using GlobalBank.BranchSystems.Properties;
using GlobalBank.Infrastructure.Interface;
using GlobalBank.Infrastructure.Interface.BusinessEntities;
using GlobalBank.Infrastructure.Interface.Services;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

namespace GlobalBank.BranchSystems.Module.Views
{
	public class FindCustomerViewPresenter : Presenter<IFindCustomerView>
	{
		private ICustomerFinderService _customerFinderService;

		[InjectionConstructor]
		public FindCustomerViewPresenter
			(
			[ServiceDependency] ICustomerFinderService customerFinderService
			)
		{
			_customerFinderService = customerFinderService;
		}

		public void FindCustomer(
			string firstName, string lastName, string middleInitial,
			string ssn, string street, string city,
			string state, string zip, string homePhone,
			string workPhone, string cellPhone, string email)
		{
			Customer customer =
				CreateCustomer(firstName, lastName, middleInitial, ssn, street, city, state, zip, homePhone, workPhone, cellPhone,
				               email);

			Customer[] result = InnerFindCustomer(customer);

			if (result == null || result.Length == 0)
			{
				View.ShowMessage(Resources.NoCustomerFoundMessage);
			}
			else
			{
				if (WorkItem.Items.Contains("FindCustomerResponse"))
					WorkItem.Items.Remove(WorkItem.Items.Get("FindCustomerResponse"));
				WorkItem.Items.Add(result, "FindCustomerResponse");

				FindCustomerResultsView resultsView = WorkItem.Items.AddNew<FindCustomerResultsView>();
				GetViewWorkspace().Show(resultsView);
			}
		}

		private IWorkspace GetViewWorkspace()
		{
			IWorkspaceLocatorService locator = WorkItem.Services.Get<IWorkspaceLocatorService>();
			if (locator != null)
				return locator.FindContainingWorkspace(WorkItem, View);
			return null;
		}

		private Customer[] InnerFindCustomer(Customer customer)
		{
			try
			{
				return _customerFinderService.FindCustomer(customer);
			}
			catch
			{
				View.ShowMessage(Resources.CustomerFinderServiceFailure);
			}
			return null;
		}

		private Customer CreateCustomer(string firstName, string lastName, string middleInitial, string ssn, string street,
		                                string city, string state, string zip, string homePhone, string workPhone,
		                                string cellPhone, string email)
		{
			Customer customer = new Customer();
			customer.FirstName = firstName;
			customer.LastName = lastName;
			customer.MiddleInitial = middleInitial;
			customer.SocialSecurityNumber = ssn;
			customer.Addresses = new Address[] {new Address()};
			customer.Addresses[0].Address1 = street;
			customer.Addresses[0].City = city;
			customer.Addresses[0].StateProvince = state;
			customer.Addresses[0].PostalZipCode = zip;

			customer.EmailAddresses = new EmailAddress[] {new EmailAddress()};
			customer.EmailAddresses[0].Address = email;

			customer.PhoneNumbers = new PhoneNumber[] {new PhoneNumber(), new PhoneNumber(), new PhoneNumber()};
			customer.PhoneNumbers[0].PhoneType = PhoneType.Work;
			customer.PhoneNumbers[0].Number = workPhone;
			customer.PhoneNumbers[1].PhoneType = PhoneType.Home;
			customer.PhoneNumbers[1].Number = homePhone;
			customer.PhoneNumbers[2].PhoneType = PhoneType.Mobile;
			customer.PhoneNumbers[2].Number = cellPhone;
			return customer;
		}

		public void OnCancel()
		{
			CloseView();
			WorkItem.Terminate();
		}
	}
}
